package com.example.zqm.git72.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.Adapter.CommentAdapter;
import com.example.zqm.git72.Adapter.MasterVideoListAdapter;
import com.example.zqm.git72.JavaBean.DetailShaiTu;
import com.example.zqm.git72.Model.PhotoModel;
import com.example.zqm.git72.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class PhotoDetailActivity extends AppCompatActivity {
    String id;
    public DetailShaiTu detailShaiTu;
    private TextView title;
    private ImageView thumb0;
    private ImageView thumb1;
    private ImageView thumb2;
    private TextView username;
    private TextView time;
    private EditText wirte;
    private Button send;
    private RecyclerView recyclerView;
    private List<DetailShaiTu.CommentEntity> comment;
    String shaitu_id;
    String userid;
    String reply_userid;
    String reply;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shaitudetail);
        recyclerView =(RecyclerView) findViewById(R.id.comment);
        recyclerView.setLayoutManager(new LinearLayoutManager(PhotoDetailActivity.this));
        intNN();
        intiView();
        initGG();

    }
    public  void  intiView(){
        title = (TextView)findViewById(R.id.title);
        username=(TextView)findViewById(R.id.user_name);
        time=(TextView)findViewById(R.id.update_time);
        thumb0=(ImageView)findViewById(R.id.thumb0);
        //thumb1=(ImageView)findViewById(R.id.thumb1);
       // thumb2=(ImageView)findViewById(R.id.thumb2);
        wirte=(EditText)findViewById(R.id.wirte);
        send=(Button)findViewById(R.id.send);
    }
    protected void intNN(){
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        id=bundle.getString("id");
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        reply_userid=sharedPreferences.getString("user_id","");
        PhotoModel photoModel =new PhotoModel();
        Call<DetailShaiTu> call = photoModel.getPhotoById(id);
        call.enqueue(new Callback<DetailShaiTu>() {
            @Override
            public void onResponse(Call<DetailShaiTu> call, Response<DetailShaiTu> response) {
                detailShaiTu = response.body();
                //username.setText(detailShaiTu.getUserid());
                username.setText(detailShaiTu.getShaier().getUser_name());
                time.setText(detailShaiTu.getUpdate_time());
                title.setText("#"+detailShaiTu.getTitle()+"#");

                String picUrl = "http://172.24.10.175/workout/Uploads/" + detailShaiTu.getThumb0();
                Picasso.with(PhotoDetailActivity.this).load(picUrl.toString())
                        .resize(400,100).into(thumb0);

                comment= detailShaiTu.getComment();
                recyclerView.setAdapter(new CommentAdapter(PhotoDetailActivity.this,comment));//设置适配器

            }
            @Override
            public void onFailure(Call<DetailShaiTu> call, Throwable t) {

            }
        });
  }

  public void  initGG(){
      send.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              shaitu_id=id;
              userid=username.getText().toString();
              reply= wirte.getText().toString();
              PhotoModel photoModel = new PhotoModel();
              Call<String> call = photoModel.createreply(shaitu_id,userid,reply_userid,reply);
              call.enqueue(new Callback<String>() {
                  @Override
                  public void onResponse(Call<String> call, Response<String> response) {
                      Toast.makeText(PhotoDetailActivity.this,"发表成功！",Toast.LENGTH_SHORT).show();
                      refresh();
                  }

                  @Override
                  public void onFailure(Call<String> call, Throwable t) {

                  }
              });
          }
      });
  }

 public  void refresh(){
     intNN();
 }


}
